var express = require('express');
var superagent = require("superagent")
    // var cheerio = require("cheerio")
var app = express();
var config = require("../config.json")
var Cookie = "ASP.NET_SessionId=r42wboe4zprbk25wg0tkavsj; .ASPXAUTH=B0A3B348EB5B908E1794E00EE2E17478F7F950D074ACE56EDD1D89D02E0F0D851112B73EEF42653EB64E5F51F48B1EF1B0F802CE470FE9906F738E2189726BA6E48F2BF25D1666B2180741AFC04B798CD55D532D06DD605B6B67298E0FC76D0F"
let login = "http://" + config.dev + "/Request/ashx/Logon.ashx?type=islogon&_=1626946511290"
var baseURL = "http://" + config.dev + "/Request/ashx/V_DataSource.ashx?type=getdata&dbCode="


function GetCodeData(res, baseURL) {
    superagent.get(baseURL)
        .set('Cookie', Cookie)
        .end(function(err, sres) {
            if (err) {
                return next(err);
            }
            res.send(sres.text);
        })
}

function GetCodeData(baseURL) {
    console.dir(baseURL);
    return new Promise((res, rej) => {
        superagent.get(baseURL)
            .end((err, sres) => {
                if (err) {
                    return rej(err);
                }
                res(sres)
            })
    })
}




app.get('/', function(req, res) {
    // http://localhost:88/?code=VB21072215023753
    console.dir(baseURL);
    if (req.query.code == '') {
        GetCodeData(res, login)
    } else {
        GetCodeData(res, baseURL + req.query.code)
    }

});

app.get('/weather', function(req, res) {
    // http://localhost:88/weather?city=101030100
    let city = req.query.city
    if (city == '') {
        res.send({
            text: "参数请查证city传入是否有误！"
        });
    } else {
        // GetCodeData("http://t.weather.itboy.net/api/weather/city/" + city).then((data) => {
        //     res.send(JSON.parse(data.text));
        // })
    }
});







app.listen(88, function() {
    console.log('app http://localhost:88/?code');
});